#ifndef __DATAWIDGET_H__
#define __DATAWIDGET_H__

#include <QtGui/QWidget>
#include <QtGui/QGroupBox>
#include <QtGui/QListWidget>
#include <QtGui/QTableWidget>
#include <QtGui/QPushButton>
#include <QtGui/QComboBox>
#include <QtGui/QCheckBox>
#include <QtGui/QLabel>
#include <QtGui/QTableWidgetItem>

#include <QtGui/QVBoxLayout>
#include <QtGui/QHBoxLayout>
#include <QtGui/QSpacerItem>

#include <string>

#include "database_sqlite.h"
#include "config.h"
#include "nameinput.h"

namespace verema
{

/**
*  \brief Enumeration to identify the CheckBox setting.
*/
enum checkedState
{
	nothingChecked,
	unusedChecked,
	usedChecked,
	bothChecked
};

/**
  *  \brief This class provides the text/data view of the verema tool.
  */
class DataWidget : public QWidget
{
Q_OBJECT

public:
	/**
	*  \brief Constructor
	*/
	DataWidget();

	/**
	*  \brief Destructor
	*/
	~DataWidget();

	void addNewDatabase(QString filename);
	
public slots:
	
	/**
	*  \brief Reimplemented show function to execute code when this widget is shown.
	*/
	void show();

private slots:
	/**
	*  \brief This slot fills the subunit combobox whenever a unit was chosen
	*/
	void unitActivated();

	/**
	*  \brief This slot fills the table widget whenever a subunit was chosen
	*/
	void subunitActivated();

	/**
	*  \brief This slot saves an item whenever an item was changed
	*/
	void itemChanged(QTableWidgetItem *item);

	/**
	*  \brief This slot saves an item whenever a combobox was changed
	*/
	void comboChanged();

	/**
	*	\brief This slot sets the m_intListMode variable when the filter state changes.
	*	\param state Current state of checkbox (see enum QCheckBox::ToggleState)
	*/
	void chkInuseChanged(int state);

	/**
	*	\brief This slot sets the m_intListMode variable when the filter state changes.
	*	\param state Current state of checkbox (see enum QCheckBox::ToggleState)
	*/
	void chkNotinuseChanged(int state);

	/**
	*  \brief This slot shows an input dialog to specify the name of an new unit.
	*/
	void addUnit();

	/**
	*  \brief This slots adds a new unit to the database.
	*/
	void appendUnit();

	/**
	*  \brief This slot deletes the current active unit whenever btn_deleteUnit was clicked.
	*/
	void deleteUnit();

	/**
	*  \brief This slot shows an input dialog to specify the name of an new subunit.
	*/
	void addSubunit();

	/**
	*  \brief This slots adds a new subunit to the database.
	*/
	void appendSubunit();

	/**
	*  \brief This slot deletes the current active subunit whenever btn_deleteSubunit was clicked.
	*/
	void deleteSubunit();

	/**
	*  \brief This slot deletes the current chosen item whenever btn_addUnit was clicked.
	*/
	void deleteItem();

private:
	/**
	*  \brief This function removes all table contenst from table widget.
	*/
	void deleteAllTableItems();

	/**
	*  \brief This function reloads all table contents in table widget
	*/
	void refreshTable();

	/**
	*  \brief This function labels (or relabels) all action Buttons
	*/
	void nameActionButtons();

	/**
	*  \brief This function refreshes the item_vector depending on current m_intListMode.
	*/
	void filterItemVector();

	/**
	*  \brief Updates the unit combobox. Useful when units were added or deleted
	*  \param startAtFirst true, if the first item should be activated. False, if the last Item should be activated.
	*/
	void updateUnitCombo(bool startAtFirst);

	QVBoxLayout vbl_mainlayout;
	QHBoxLayout hbl_top, hbl_bottom;

    QTableWidget tbl_items;

	QGroupBox grb_select, grb_filter, grb_action;

    QVBoxLayout vbl_select, vbl_filter;
    QHBoxLayout hbl_action;
    QVBoxLayout vbl_actionLeft, vbl_actionRight;

    QLabel lbl_unit, lbl_subunit;
    QComboBox cmb_unit, cmb_subunit;
    QCheckBox chk_inuse, chk_notinuse;

    QPushButton btn_newUnit, btn_deleteUnit, btn_newSubunit, btn_deleteSubunit;
    QLabel lbl_spacer;
    QPushButton btn_deleteItem;

	QList<QTableWidgetItem*> m_fields;
	QList<QTableWidgetItem*> m_tableItems;
	QList<QComboBox*> m_tableCombos;

	int m_intListMode; // see enum "checkedState"

	Database_Sqlite m_dataBase;
	nameInput newUnitDialog, newSubunitDialog;

	std::vector<Unit> unit_vector;
	std::vector<Subunit> subunit_vector;
	std::vector<Item> item_vector;
};

} // namespace verema

#endif // __DATAWIDGET_H__
